<template>
  <div>
    <el-table :data="get_userList" border style="width: 100%">
      <el-table-column prop="uid" label="用户编号" width="180"></el-table-column>
      <el-table-column prop="username" label="用户名称" width="180"></el-table-column>
      <el-table-column prop="rolename" label="所属角色"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="item">
          <el-button type="primary" plain size="small" v-if="item.row.status == 1">启用</el-button>
          <el-button type="danger" plain size="small" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="item">
          <el-button type="primary" size="small" @click="edit(item.row.uid)">编辑</el-button>
          <el-button type="danger" size="small" @click="delItem(item.row.uid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- total:总条目数 -->
    <el-pagination background layout="prev, pager, next" :total="get_count" 
    :page-size="2"
    @current-change="changePage"
    ></el-pagination>
  </div>
</template>

<script>
import { mapGetters, mapActions,mapMutations } from "vuex";
import { userdelete } from "@/request/api.js";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      get_userList: "user/get_userList",
      get_count:"user/get_count" //获取总条数
    })
  },
  methods: {
    ...mapActions({
      action_userList: "user/action_userList", //获取管理员列表的
      action_count:"user/action_count", //获取总条数
    }),
    ...mapMutations({
      mutation_page:"user/mutation_page"
    }),

    //点击删除按钮
    delItem(uid) {
      this.$confirm("是否删除该角色", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            let res = await userdelete({ uid });
            if (res.code == 200) {
              //提示，更新数据（重新请求数据）
              this.$message.success(res.msg);
              this.action_userList();
              //重新请求页码
              this.action_count();
            } else {
              this.$message.info(res.msg);
            }
          } catch (err) {
            this.$message.error(err);
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    //点击编辑
    edit(uid) {
      //通知父组件显示弹框，弹框里面要有对应的内容  id
      this.$emit("edit", uid);
    },
    //page页码发生变化时触发
    changePage(e){ //e当前是第几页
      //1.改变页码
      this.mutation_page(e);
      //2.更新页面 -- 重新获取数据
      this.action_userList();
    }
  },
  //页面一加载就请求数据
  mounted() {
    this.action_userList();
    this.action_count();
  }
};
</script>

<style scoped>
.el-pagination{
  float: right;
  margin: 20px 0;
}
</style>